﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace Workflow.Project
{
    public partial class NodeTemp : System.Web.UI.Page
    {
        public List<PJNode> resultList = new List<PJNode>();
        public List<PJNode> mouldList = new List<PJNode>();
        private PJNodeManager objPJNodeManager = new PJNodeManager();
        private WorkflowManager objWorkflowManager = new WorkflowManager();
        private UserTeamRoleManager objUserTeamRoleManager = new UserTeamRoleManager();
        public int workParentId;
        public int pjCount;
        public int statusNo;
        public int listCount;
        public int fromPage;

        protected void Page_Load(object sender, EventArgs e)
        {
            workParentId = Convert.ToInt32(Request.QueryString["WorkParentId"]);
            fromPage = Convert.ToInt32(Request.QueryString["FromPage"]);
            try
            {
                resultList = objPJNodeManager.GetPJNodeInfo(workParentId, null, "PJNodesTemp");
                listCount = resultList.Count;
                ShowInformation(resultList);
                Btn1.Visible = false;
                txtReason.Text = resultList[0].Reason;
            }
            catch (Exception ex)
            {

                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Response.Write(msg);
            }
        }

        protected void Btn4_Click(object sender, EventArgs e)
        {
            try
            {
                objPJNodeManager.DeletePJNode(workParentId, null, "PJNodesTemp");
                string msg = string.Format("<script>alert('删除成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {

                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Response.Write(msg);
            }

        }

        protected void Btn3_Click(object sender, EventArgs e)
        {
            try
            {
                objPJNodeManager.UpdateInfo(workParentId, "1", "PJNodesTemp");
                resultList = objPJNodeManager.GetPJNodeInfo(workParentId, null, "PJNodesTemp");
                foreach (var item in resultList)
                {
                    objPJNodeManager.UpdateInfo(item, "PJNodes", item.PJId);
                }
                
                string msg = string.Format("<script>alert('同步成功！')</script>");
                string str = string.Format("<script>window.close();</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "", str);
            }
            catch (Exception ex)
            {

                string msg = string.Format("<script>alert('{0}');</script>", ex.Message.ToString());
                Response.Write(msg);
            }
        }

        protected void Search_Click(object sender, EventArgs e)
        {
            resultList = objPJNodeManager.GetPJNodeInfo(-1, txtNo.Value.Trim().ToUpper(), "PJNodes");
            listCount = resultList.Count;
            ShowInformation(resultList);
            if (resultList.Count > 0)
            {
                
                txtData.Text = "未同步";
                Search.Visible = false;
                Btn4.Visible = false;
                Btn2.Visible = false;
            }
            else
            {
                string msg = string.Format("<script>alert('没有找到该项目号节点信息！')</script>");
                Page.ClientScript.RegisterStartupScript(Page.GetType(), "alert", msg);
            }
            
        }
        void ShowInformation(List<PJNode> List)
        {
            pjCount = objUserTeamRoleManager.GetUserInfoTeam(Session["FullName"].ToString(), 6, 9);
            statusNo = objWorkflowManager.GetWorkflowStatues(workParentId);
            if (resultList.Count > 0)
            {
                txtNo.Value = resultList[0].PJId;
                txtNo.Disabled = true;
                txtData.Text = resultList[0].Statues == "1" ? "已同步" : "未同步";
                if (fromPage == 0 || statusNo != 100)
                {
                    if (pjCount > 0 && statusNo < 1)
                    {
                        Btn1.Visible = true;
                        Btn2.Visible = true;
                        Btn4.Visible = true;
                        txtReason.Enabled = true;
                    }
                    else if ((pjCount > 0 && statusNo == 2))
                    {
                        Btn3.Visible = true;
                    }
                }
                if (statusNo == 100)
                {
                    Btn3.Visible = true;
                }
            }
            else
            {
                //mouldList = objPJNodeManager.GetPJNodeMould();
                txtData.Text ="未同步";
                if (pjCount > 0 && statusNo < 1)
                {
                    Search.Visible = true;
                }
                else if ((pjCount > 0 && statusNo == 2))
                {
                    Btn3.Visible = true;
                }
            }
        }
    }
}